package MapReduce.Demo7_TextSort;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * @Author lixinlei
 * @Date 2023/3/20 11:07
 */
public class TextSortComparator extends WritableComparator {


    public TextSortComparator() {
        super(Text.class,true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {

        Text aBean = (Text)a;
        Text bBean = (Text)b;


        String aStr = aBean.toString();
        String bStr = bBean.toString();


        String aSecondField = aStr.split("-")[1];
        String bSecondField = bStr.split("-")[1];

//        return aSecondField.compareTo(bSecondField);

        int aSecondFieldInt = Integer.parseInt(aSecondField);
        int bSecondFieldInt = Integer.parseInt(bSecondField);

        return aSecondFieldInt-bSecondFieldInt;

    }
}
